package org.smartboot.flow.springboot.extension;

import org.smartboot.flow.spring.extension.EngineInitializer;
import org.smartboot.flow.manager.reload.Reloader;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * smart flow EngineInitializer auto config
 *
 * @author huqiang
 * @since 2022/12/15 16:32
 */
@Configuration
@ConfigurationProperties(prefix = "smart.flow.initialize")
@ConditionalOnProperty(name = "smart.flow.initialize.engines")
@AutoConfigureAfter(FlowReloadConfiguration.class)
public class FlowInitializerConfiguration {

    /**
     * Initializer engines, multiple engine name split with ,
     */
    private String engines;

    @Bean(value = "springboot-engine-initializer", initMethod = "start")
    @ConditionalOnBean(Reloader.class)
    @ConditionalOnMissingBean(EngineInitializer.class)
    public EngineInitializer getInitializer(Reloader reloader) {
        EngineInitializer initializer = new EngineInitializer();
        initializer.setReloader(reloader);
        initializer.setEngines(Arrays.asList(engines.split(",")));
        return initializer;
    }


    public void setEngines(String engines) {
        this.engines = engines;
    }
}
